package com.thinkinghead.mahjongrecorder.adaptor;



import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.thinkinghead.mahjongrecorder.R;
import com.thinkinghead.mahjongrecorder.model.Record;
import com.thinkinghead.mahjongrecorder.utils.TypeFaceManager;

public class GameListItemAdaptor extends BaseAdapter {
	private Context context;
	private List<Record> gameRecordList;
	private int selectedPos = -1;
	private Animation fadeInAnimation;
	
	public GameListItemAdaptor(Context context, List<Record> gameRecordList) {
		this.context = context;
		this.gameRecordList = gameRecordList;
		this.fadeInAnimation = AnimationUtils.loadAnimation(this.context, R.anim.fade_in);
	}
	
	public void setRecordList(List<Record> gameRecordList) {
		this.gameRecordList = gameRecordList;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		LayoutInflater mInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item,
					null);
			holder = new ViewHolder();
			
			if(convertView.findViewById(R.id.icon)!=null){
				holder.imageView = (ImageView) convertView.findViewById(R.id.icon);				
			}
			holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
			holder.txtBody = (TextView) convertView.findViewById(R.id.body);
			holder.txtWinner = (TextView) convertView.findViewById(R.id.winner);
			fnSetSelectedColor(convertView,position);
			convertView.setTag(holder);
		} else {
			fnSetSelectedColor(convertView,position);
			holder = (ViewHolder) convertView.getTag();
		}

		Record item = (Record) getItem(position);
		Typeface tf = TypeFaceManager.getTypeFace(this.context.getAssets());
		if(tf!=null){
			holder.txtTitle.setTypeface(tf);			
		}
		
		String todayStr = (String) android.text.format.DateFormat.format("yyyy-MM-dd hh:mm:ss", item.getRecordDT());
		holder.txtTitle.setText(todayStr);
		// holder.txtTitle.setTypeface(null, Typeface.BOLD);
		
		holder.txtBody.setText(item.getDescription());
		holder.txtWinner.setText(item.getCreatedBy().getShortName());
		if(holder.imageView!=null){
			holder.imageView.setImageResource(item.getImageID());			
		}
		return convertView;
	}

	private void fnSetSelectedColor(View convertView, int position) {
		if(selectedPos == position){
			convertView.setBackgroundColor(Color.CYAN);
        }else{
        	convertView.setBackgroundColor(Color.WHITE);
        }
	}

	public long getItemId(int position) {
		return position;
	}

	public Record getItem(int position) {
		return gameRecordList.get(position);
	}

	public int getCount() {
		return gameRecordList.size();
	}

	/* private view holder class */
	private class ViewHolder {
		ImageView imageView;
		TextView txtTitle;
		TextView txtBody;
		TextView txtWinner;
	}
	
	public Record getSelectedItem(){
		if(selectedPos>=0){
			Record b = (Record) getItem(selectedPos);
			return b;			
		}
		return null;
	}
	
	public int getSelectedIndex(){
		return selectedPos;
	}
	
	public void setSelectedPosition(int pos){
		selectedPos = pos;
		notifyDataSetChanged();
	}
	
	public void addItem(Record li){
		this.gameRecordList.add(li);
		notifyDataSetChanged();
	}
	
	public void addItems(Record li){
		this.gameRecordList.add(li);
	}
	
	public void update(){
		this.notifyDataSetChanged();
		this.notifyDataSetInvalidated();
		notifyDataSetChanged();
	}
	
	public void removeAll(){
		while(gameRecordList.size()>0){
			gameRecordList.remove(0);
		}
		notifyDataSetChanged();
	}
}